import streamlit as st
import utills
import os
import pandas as pd
import pygwalker as pyg
import subprocess

from pygwalker.api.streamlit import StreamlitRenderer
import extra_streamlit_components as stx
from annotated_text import annotated_text
from streamlit_image_coordinates import streamlit_image_coordinates
from testforpdf import generate
# Set page configuration to hide the sidebar on load
st.set_page_config(layout="wide")
upload_folder = "./upload/video_input/"
landmarks_folder = "./landmarks"
visualize_folder = "./visualize"
upload_folder = "./upload/images"

if "WelcomeState" not in st.session_state:
    st.session_state.WelcomeState = "Unupload"

def clear_folder(folder_path):
    # 获取文件夹中的所有文件和子文件夹
    files = os.listdir(folder_path)
    if not files:  # 如果文件夹为空
        return "文件夹内无文件"
    for file in files:
        # 构建文件/文件夹的完整路径
        file_path = os.path.join(folder_path, file)
        # 如果是文件，则删除
        if os.path.isfile(file_path):
            os.remove(file_path)
        # 如果是文件夹，则递归清空
        elif os.path.isdir(file_path):
            clear_folder(file_path)
headCol = st.columns([1,1,1])
with headCol[1]:
     image_url = ""
     stx.bouncing_image(image_source=image_url, animate=False, animation_time=1500, height=300, width=250)




mainCol = st.columns([6,1])
with mainCol[0]:
    uploaded_file = st.file_uploader("")
    uploaded_filename = None  # 初始化变量用于存储上传文件的文件名

    # 如果有文件被上传
    if uploaded_file is not None:
        # 将文件保存到指定路径
        file_path = os.path.join(upload_folder, uploaded_file.name)
        with open(file_path, "wb") as f:
            f.write(uploaded_file.getbuffer())
        
        # 存储上传文件的文件名
        uploaded_filename = uploaded_file.name
        st.session_state.WelcomeState = "uploaded"
if st.session_state.WelcomeState=="uploaded":
    #执行图像指纹部分
    score = utills.trufor(f"./upload/images/{uploaded_file.name}")
    with open("/home/xinan-works/truefor_num.txt","w") as file:
        file.write(f"{score}"[0:-1])
    #执行SIFT检测部分    
    utills.Do_SIFTForgeryDetector(f"./upload/images/{uploaded_file.name}")
    #执行噪声方差提取部分
    utills.detect_noise_variance(f"./upload/images/{uploaded_file.name}",blockSize=32)
    #执行图像错误水平分析部分
    utills.ela_analysis(f"./upload/images/{uploaded_file.name}")
    #执行低位通道提取子模块
    utills.Do_ExtractLSB(f"./upload/images/{uploaded_file.name}")
    #生成pdf
    generate()
    # 读取本地 PDF 文件
    with open("/home/xinan-works/complex_layout_example.pdf", "rb") as file:
        pdf_data = file.read()

    # 创建下载按钮
    st.download_button(
        label="下载检测报告",
        data=pdf_data,
        file_name="检测报告.pdf",
        mime="application/pdf"
    )